<template>
    <div class="login_container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form
                    class="login_form"
                    :model="loginForm"
                    :rules="rules"
                    ref="loginFormVC"
                >
                    <h1>Hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item prop="username">
                        <el-input
                            :prefix-icon="User"
                            v-model="loginForm.username"
                            placeholder="请输入用户名"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input
                            :prefix-icon="Lock"
                            type="password"
                            placeholder="请输入密码"
                            show-password
                            v-model="loginForm.password"
                        ></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button
                            :loading="loading"
                            @click="login(loginForm)"
                            class="login_btn"
                            type="primary"
                        >
                            登录
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue' //引入图标
import { ref, reactive } from 'vue'
import useUserStore from '@/store/modules/user' //引入用户仓库
import { useRouter } from 'vue-router' //引入路由
import { ElNotification, FormInstance } from 'element-plus'
import type { loginFormData } from '@/api/user/type'
import { getGreeting } from '@/utils/greeting_time'

let loading = ref(false) //登录按钮的loading状态

//表单收集的账号和密码
let loginForm = reactive({
    username: 'admin',
    password: '111111',
})

//自定义表单校验规则
const validatorUsername = (_rule: any, value: any, callback: any) => {
    if (!value) return callback(new Error('请输入用户名'))
    if (value.length < 3 || value.length > 10)
        return callback(new Error('长度在 3 到 10 个字符'))
    callback()
}

//表单校验规则
let rules = {
    username: [
        // { requred: true, min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'change' }
        { trigger: 'change', validator: validatorUsername }, //自定义校验规则
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
    ],
}

let userStore = useUserStore() //获取用户仓库
let $router = useRouter() //获取路由
let loginFormVC = ref<FormInstance>()

const login = async (data: loginFormData) => {
    if (!loginFormVC.value) return
    await loginFormVC.value.validate() //保证表单校验通过，

    loading.value = true //登录按钮的loading状态
    //通知仓库发起登录请求
    //可以使用try catch，也可以用.then()方法
    try {
        await userStore.userLogin(data)
        //登录成功后跳转到首页
        $router.push('/')
        //登录成功提示信息
        ElNotification({
            title: `HI, ${getGreeting()}好`,
            message: '欢迎回来',
            type: 'success',
        })
        loading.value = false //登录成功后，登录按钮的loading状态
    } catch (error) {
        loading.value = false //登录按钮的loading状态
        //登录失败提示信息
        ElNotification({
            title: '登录失败',
            message: (error as Error).message,
            type: 'error',
        })
    }
}
</script>

<style scoped lang="scss">
.login_container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/background.jpg') no-repeat center/cover;
}

.login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    padding: 40px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    background: url('@/assets/images/login_form.png') no-repeat center/cover;

    h1 {
        font-size: 40px;
        color: #fff;
    }

    h2 {
        font-size: 20px;
        color: #fff;
        margin: 20px 0;
    }

    .login_btn {
        width: 100%;
    }
}
</style>
